package com.vframework.system.user.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.vframework.base.exception.BaseException;
import com.vframework.base.model.BaseBean;
import com.vframework.base.service.impl.BaseServiceImpl;
import com.vframework.system.role.model.RoleBean;
import com.vframework.system.user.mapper.UserMapper;
import com.vframework.system.user.model.UserBean;
import com.vframework.system.user.service.UserService;

@Service
public class UserServiceImpl extends BaseServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    public Integer getCount(Map<String, Object> paramMap) throws BaseException {
        return userMapper.getCount(paramMap);
    }
    
    public List<UserBean> getUserList(Map<String, Object> paramMap) throws BaseException {
        return userMapper.getUserList(paramMap);
    }

    public UserBean getUserById(String userId) throws BaseException {
        UserBean userBean = userMapper.getUserById(userId);
        if (userBean != null && userBean.getRoleIdsList() != null)
            userBean.setRoleIds(userBean.getRoleIdsList().toArray(new String[userBean.getRoleIdsList().size()]));
        return userBean;
    }

    public List<BaseBean> getRoleByOrgId(Map<String, Object> paramMap) throws BaseException {
        return userMapper.getRoleByOrgId(paramMap);
    }

    public List<BaseBean> getRoleByMap(Map<String, Object> paramMap) throws BaseException {
        return userMapper.getRoleByMap(paramMap);
    }

    public Integer getUserEditCount(UserBean userBean) throws BaseException {
        return userMapper.getUserEditCount(userBean);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void addUser(UserBean user) throws BaseException {
        userMapper.insertUser(user);
        this.setRole(user);
        if (user.getRoleSet() != null) {
            for (RoleBean role : user.getRoleSet()) {
                userMapper.insertUserRole(role);
            }
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void editUser(UserBean user) throws BaseException {
        Integer count = null;
        String account = user.getAccount();
        String oldAccount = user.getOldAccount();
        if (!account.equals(oldAccount)) {
            count = userMapper.getUserEditCount(user);
        }
        if (count == 0) {
            this.setRole(user);
            userMapper.updateUser(user);
            userMapper.deleteUserRole(user.getUserId());
            if (user.getRoleSet() != null) {
                for (RoleBean role : user.getRoleSet()) {
                    userMapper.insertUserRole(role);
                }
            }
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteUser(Map<String, Object> paramMap) throws BaseException {
        String[] ids = (String[]) paramMap.get("ids");
        if (ids == null || ids.length < 1) {
            throw new BaseException("No data to operate");
        }
        userMapper.deleteUser(paramMap);
        for (String id : ids) {
            userMapper.deleteUserRole(id);
        }
    }

    /**
     * Author:zhaorui Create At:2010-06-29 Description: [用户管理-暂停用户信息]
     * 
     * @return String
     */
    public void pauseUser(Map<String, Object> paramMap) throws BaseException {
        userMapper.pauseUserStat(paramMap);
    }

    /**
     * Author:zhaorui Create At:2010-06-29 Description: [用户管理-启动用户信息]
     * 
     * @return String
     */
    public void reStartUser(Map<String, Object> paramMap) throws BaseException {
        userMapper.startUserStat(paramMap);
    }

    /**
     * Author:shanzhitong Create At:2010-06-29 Description: [用户修改个人资料]
     */
    public void savePresonInfo(UserBean userBean) throws BaseException {
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("orgId", userBean.getOrgId());
        paramMap.put("userId", userBean.getUserId());
        paramMap.put("oldPassword", userBean.getOldPassword());
        Integer flag = userMapper.checkOldPwd(paramMap);
        if (flag == 1) {
            userMapper.personInfo(userBean);
        } else {
            throw new BaseException("输入的原密码不正确");
        }
    }

    public Integer updatePassword(Map<String, Object> paramMap) throws BaseException {
        return userMapper.updatePassword(paramMap);
    }

    /**
     * 置入角色
     * 
     * @param user
     */

    private void setRole(UserBean user) {
        String[] ids = user.getRoleIds();
        if (ids != null) {
            List<RoleBean> roleSet = new ArrayList<RoleBean>();
            for (String id : ids) {
                RoleBean role = new RoleBean();
                role.setRoleId(id);
                role.setUserId(user.getUserId());
                roleSet.add(role);
            }
            user.setRoleSet(roleSet);
        }
    }

    public Integer queryTemplateList(Map<String, Object> map) throws BaseException {
        return userMapper.queryTemplateList(map);
    }

    public UserBean getUserByName(Map<String, Object> paramMap) throws BaseException {
        return userMapper.getUserByName(paramMap);
    }
}
